﻿using Ldt.DataEngine.DataFlowParse.Model;
using LDT.CoreLibrary;
using LdtDataEngin.ModuleDetail.BaseModule;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.AnalysisLabel;
using LdtJudge.DataEngine.Common.DataSave;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.AnalysisLabelModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataSave;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CommonModule.CaseNameGenerate
{
    /// <summary>
    /// 生成案件名称
    /// </summary>
    public class CaseNameGenerateDetail: AssermBase
    {
        /// <summary>
        /// 条件排序配置
        /// </summary>
        public  ReorderModel reorderModel;

        /// <summary>
        /// 模板配置文件
        /// </summary>
        public XmlDocument xmlTemplate;
        /// <summary>
        /// 案件名称构造函数
        /// </summary>
        /// <param name="dataContext"></param>
        /// <param name="moudleName"></param>
        public CaseNameGenerateDetail(DataContext dataContext,string moudleName)
        {
            this.dataContext = dataContext;
            this.MoudleName = moudleName;
        }

        public void Extrat()
        {
            try
            {
                try
                {
                    dataContext.InteractiveShow.LogShow("进入案件名称获取服务");
                    //获取系统服务api接口
                    string systemapiurl = ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("systemapi");
                    string caseuse = "";
                    if (dataContext.DataLayerAns.CaseCausesList!=null && dataContext.DataLayerAns.CaseCausesList.Count > 0)
                    {
                        caseuse = dataContext.DataLayerAns.CaseCausesList[0].CC_ID;
                    }
                    if (base.dataContext.DataLayerOpt.SourdatModleList.Count>0)
                    {
                        XmlDocument xmlDocument = XmlUtility.DataModelByXml(base.dataContext.DataLayerOpt.SourdatModleList);
                        base.dataContext.DataLayerOpt.OptimizationXml = xmlDocument;
                    }
                    
                    dataContext.InteractiveShow.LogShow("案件名称ajmc案由:" + caseuse);
                    dataContext.InteractiveShow.LogShow("案件名称ajmcSD_CaseOnlyID:" + dataContext.DataCommon.CaseId);
                    dataContext.InteractiveShow.LogShow("案件名称ajmc案件类型:" + dataContext.DataCommon.CaseType);
                    LabelClass analysisLabelClass = new LabelClass();
                    string name = analysisLabelClass.CaseCauseName
                         (caseuse, dataContext.DataCommon.CaseId,
                         dataContext.DataCommon.CaseType, dataContext.DataLayerOpt.OptimizationXml.OuterXml, systemapiurl);
                    dataContext.InteractiveShow.LogShow("案件名称ajmc:" + name);
                    //获取配置项
                    var configModel = dataContext.DataLayerSys.GetConfigModel(MoudleName);
                    ConfigModel caseNameConfig = configModel.ConfigDetail.FirstOrDefault(x => x.Code.ToLower()=="casename");

                    if (caseNameConfig!=null && !string.IsNullOrWhiteSpace(caseNameConfig.ConfigContent))
                    {
                        string elementcofigstr = caseNameConfig.ConfigContent;
                        AnalysisLabelListModel analysisLabelListModel = XmlUtility.DeserializeToObject<AnalysisLabelListModel>(elementcofigstr);
                        var labelconfig = analysisLabelListModel.analysisLabes.FirstOrDefault(a => a.Casetype == dataContext.DataCommon.CaseType);

                        XmlDocument xmlDocument = new XmlDocument();
                        XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() {dataContext=dataContext };
                        dataContext.DataLayerOpt.OptimizationXml = xmlUtilCommon.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList, "");

                        DataSaveOrder dataSaveOrder = new DataSaveOrder();
                        dataSaveOrder.dataContext = dataContext;
                        dataSaveOrder.OrderEvidDataModel(dataContext.DataLayerOpt.SourdatModleList);
                        XmlDocument xml = dataSaveOrder.OrderModel(xmlTemplate, reorderModel, dataContext.DataLayerOpt.SourdatModleList);
                        if (xml != null && !string.IsNullOrEmpty(xml.OuterXml))
                        {
                            dataContext.DataLayerOpt.OptimizationXml = xml;
                        }
                        else
                        {
                            dataContext.DataLayerOpt.OptimizationXml = xmlUtilCommon.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList, "");
                        }
                        if (dataContext.DataLayerOpt.OptimizationXml != null)
                        {
                            XmlUtilCommon.EscapeNodeIndexAndBlank(dataContext.DataLayerOpt.OptimizationXml);
                        }
                        xmlDocument.LoadXml(dataContext.DataLayerOpt.OptimizationXml.OuterXml);
                        XmlNode xmlNode = xmlDocument.SelectSingleNode(labelconfig.Value);
                        if (xmlNode != null)
                        {
                            if (xmlNode.Attributes[labelconfig.Name] != null)
                            {
                                xmlNode.Attributes[labelconfig.Name].Value = name;
                            }
                            else
                            {
                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute(labelconfig.Name);
                                xmlAttribute.Value = name;
                                xmlNode.Attributes.Append(xmlAttribute);
                            }
                            dataContext.DataLayerOpt.OptimizationXml = xmlDocument;
                        }

                    }
                }
                catch (Exception e)
                {
                    dataContext.InteractiveShow.LogShow("案件名称获取服务错误！！" + e.ToString());
                }
            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("保存错误！" + ex.ToString());
            }
        }

    }
}
